package com.nursinghome.management.controller;

import com.nursinghome.management.model.*;
import com.nursinghome.management.service.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/staff")
public class StaffController {

    private final UserService userService;
    private final StaffService staffService;
    private final ElderlyService elderlyService;
    private final HealthRecordService healthRecordService;
    private final MedicationService medicationService;

    public StaffController(UserService userService, StaffService staffService, ElderlyService elderlyService,
                           HealthRecordService healthRecordService, MedicationService medicationService) {
        this.userService = userService;
        this.staffService = staffService;
        this.elderlyService = elderlyService;
        this.healthRecordService = healthRecordService;
        this.medicationService = medicationService;
    }

    @GetMapping("/dashboard")
    public String dashboard(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();

        // Get current staff
        User user = userService.loadUserByUsername(username);
        Staff staff = staffService.getStaffByUser(user);

        // Get counts for dashboard
        List<Elderly> elderlyList = elderlyService.getAllElderly();

        model.addAttribute("staff", staff);
        model.addAttribute("elderlyCount", elderlyList.size());

        return "staff/dashboard";
    }

    @GetMapping("/elderly")
    public String listElderly(Model model) {
        List<Elderly> elderlyList = elderlyService.getAllElderly();
        model.addAttribute("elderlyList", elderlyList);
        return "staff/elderly";
    }

    @GetMapping("/elderly/{id}")
    public String viewElderly(@PathVariable Long id, Model model) {
        Elderly elderly = elderlyService.getElderlyById(id);
        List<HealthRecord> healthRecords = healthRecordService.getHealthRecordsByElderly(elderly);
        List<Medication> medications = medicationService.getActiveMedicationsByElderly(elderly);

        model.addAttribute("elderly", elderly);
        model.addAttribute("healthRecords", healthRecords);
        model.addAttribute("medications", medications);

        return "staff/view-elderly";
    }

    @GetMapping("/health-records/add/{elderlyId}")
    public String showAddHealthRecordForm(@PathVariable Long elderlyId, Model model) {
        Elderly elderly = elderlyService.getElderlyById(elderlyId);
        HealthRecord healthRecord = new HealthRecord();
        healthRecord.setElderly(elderly);

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();
        User user = userService.loadUserByUsername(username);
        Staff staff = staffService.getStaffByUser(user);
        healthRecord.setRecordedBy(staff);

        model.addAttribute("healthRecord", healthRecord);
        model.addAttribute("elderly", elderly);

        return "staff/add-health-record";
    }

    @PostMapping("/health-records/add")
    public String addHealthRecord(@ModelAttribute("healthRecord") HealthRecord healthRecord) {
        healthRecordService.createHealthRecord(healthRecord);
        return "redirect:/staff/elderly/" + healthRecord.getElderly().getId();
    }

    @GetMapping("/medications/add/{elderlyId}")
    public String showAddMedicationForm(@PathVariable Long elderlyId, Model model) {
        Elderly elderly = elderlyService.getElderlyById(elderlyId);
        Medication medication = new Medication();
        medication.setElderly(elderly);
        medication.setActive(true);

        model.addAttribute("medication", medication);
        model.addAttribute("elderly", elderly);

        return "staff/add-medication";
    }

    @PostMapping("/medications/add")
    public String addMedication(@ModelAttribute("medication") Medication medication) {
        medicationService.createMedication(medication);
        return "redirect:/staff/elderly/" + medication.getElderly().getId();
    }

    @GetMapping("/profile")
    public String profile(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();
        User user = userService.loadUserByUsername(username);
        Staff staff = staffService.getStaffByUser(user);

        model.addAttribute("user", user);
        model.addAttribute("staff", staff);

        return "staff/profile";
    }

    @PostMapping("/profile/update")
    public String updateProfile(@ModelAttribute("staff") Staff staff, @ModelAttribute("user") User user) {
        userService.updateUser(user);
        staffService.updateStaff(staff);
        return "redirect:/staff/profile?updated";
    }
}